package com.plm.mqshequ.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * @author Limi
 * @create 2019-01-17 20:56
 */
public class StrUtils {

    /**
     * 获取唯一ID
     * @return
     */
    public static String uuid(){
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-","");
        return uuid;
    }

    /**
     * 随机生成6位数字密码
     * @return String
     */
    public static String randomPass(){
        String password = "";
        Random random = new Random();
        for (int i=0;i<6;i++) {
            password += random.nextInt(10);
        }
        return password;
    }

    /**
     * 随机生成4位验证码
     * @return
     */
    public static String randomCode(){
        String code = "abcdefghijklmnopqrstuvwxyz";
        String activeCode = "";
        Random random = new Random();
        for (int i=0;i<4;i++) {
            activeCode += code.charAt(random.nextInt(26));
        }
        return activeCode;
    }

    /**
     * 生成计算验证码
     */
    public static Map authcode(){
        String[] operator = {"+", "-", "x"};
        Random random = new Random();
        String authcode = "";
        int result = 0;
        for (int i=0;i<3;i++) {
            if (i==1) {
                int index = random.nextInt(3);
                authcode += operator[index];
                continue;
            }
            authcode += random.nextInt(10);
        }

        String[] codes = authcode.split("\\+|-|x");
        if (authcode.contains("+")) {
            result = Integer.valueOf(codes[0]) + Integer.valueOf(codes[1]);
        } else if (authcode.contains("-")) {
            int one = Integer.valueOf(codes[0]);
            int second = Integer.valueOf(codes[1]);
            if (one < second) {
                one += 10;
            }
            result = one - second;
            authcode = one +"-" + second;
        } else {
            result = Integer.valueOf(codes[0]) * Integer.valueOf(codes[1]);
        }
        authcode += "=?";
        Map<String,String> map = new HashMap<>();
        map.put("authcode", authcode);
        map.put("result", Integer.toString(result));
        return map;
    }

}
